Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use nostr as cache system #1362

Merged
merged 34 commits into from
Sep 6, 2024
Merged

Use nostr as cache system #1362

merged 34 commits into from
Sep 6, 2024

Conversation

KoalaSat
Copy link
Member

@KoalaSat KoalaSat commented Jul 1, 2024

What does this PR do?

This is a first POC. I'm exploring to use nostr not as primary DB but as a cache system of a decentralized network such as the federation of Robosats, right now a client has to directly connect to every single coordinator book to fetch its specific orders.

Centralizing this system would create a single point of failure, so I'm exploring a federation cache network, where the full book is aditioanlly stored and maintained by coordinators, allowing users to initially connect just to 2-3 coordinators. any feedback is welcome 🙂

For now this PR just creates a coordinator relay and sends an event every time a new order is created. The note follows this on going NIP nostr-protocol/nips#1331

As additional win, this will allow Robosats to quickly integrate others' platforms orders with just a single implementation

Federation cache

Every relay of the federation will be in sync with the others, containing all orders available in the federation:

flowchart TD;
    classDef coordinator fill:#F7931A
    classDef nostr fill:#8e30eb
    subgraph 1
    Coor1:::coordinator-->Relay1;
    end
    subgraph 2
    Coor2:::coordinator-->Relay2;
    end
    subgraph 3
    Coor3:::coordinator-->Relay3;
    end

    Relay1:::nostr<-->Relay2;
    Relay2:::nostr<-->Relay3;
    Relay3:::nostr<-->Relay1;
    Relay2<-->ExternalRelay;

    Relay1-->Client;
    Relay2-->Client;
Loading

The federation will keep a strong negentropy circle between their relays, but any other external relay will be able to fetch or create new order events. Because it's just cache, we can leave the creation of events open to anyone, if it becomes a spam problem, it can be cleaned up automatically by coordinators.

Clients will always send requests filtering by coordinators' public keys, that way spam or fake orders will never reach to the user.

How to test

Publish an order. Connect your WS tools to ws://localhost:12596/nostr and send:

["REQ", "111", {"kinds":[38383]}]

You should get in return:

["EVENT", "111", <your order note>]
["EOSE","111"]  # End of response

Any change on the order should remove previous note and create a new one (identified over time by its d tag). To keep privacy, Robosats will only have 2 order statuses: pending and success.

Checklist before merging

  • Install pre-commit and initialize it: pip install pre-commit, then pre-commit install. Pre-commit installs git hooks that automatically check the codebase. If pre-commit fails when you commit your changes, please fix the problems it points out.

@KoalaSat KoalaSat force-pushed the use-nostr-as-cache-system branch 2 times, most recently from 8d8e716 to fa90ccc Compare July 8, 2024 12:41
@jerryfletcher21
Copy link
Contributor

jerryfletcher21 commented Jul 12, 2024

@KoalaSat nice idea, and really needed once the number of coordinators starts to grow. :)
An other thing that we will have to change related to this improvement is that in the main client when creating a robot, the robot is created in every coordinator, again making a requests to each of them. This is not really needed since the robot will then be used on just one coordinator.

@KoalaSat
Copy link
Member Author

@KoalaSat nice idea, and really needed once the number of coordinators starts to grow. :) An other thing that we will have to change related to this improvement is that in the main client when creating a robot, the robot is created in every coordinator, again making a requests to each of them. This is not really needed since the robot will then be used on just one coordinator.

The reason behind is that wherever the token comes from, we ask for coordinators if the token exists, in case there are active or past orders for it. Certainty, we know when the token is created but that would involve to fetch the coordinator every time the user tries to create an order, affecting the waiting time (unknown robots can´t create an order).

It's tricky to find an efficient way and don´t affect other parts, specially because the app/api assumes my robot is known, and for example, we would have to handle it in case the user visits their profile.

Anyways itś good to raise this topic from time to time to figure out if there are better ways

@jerryfletcher21
Copy link
Contributor

In roboauto I have that when creating a robot, the user have to specifiy a coordinator, and when recovering a robot, together with the secret also the coordinator have to be specified.
A similar solution could also be implemented in the main client, especially once the number of coordinators starts to grow.
It can also be added an additional button that searches across the coordinator if the robot with the specified secret is present, to be used just when the user is recovering a robot and does not remember his coordinator.
In this way except when pressing this button the client never makes a requests to every coordinator.

@KoalaSat
Copy link
Member Author

In roboauto I have that when creating a robot, the user have to specifiy a coordinator, and when recovering a robot, together with the secret also the coordinator have to be specified. A similar solution could also be implemented in the main client, especially once the number of coordinators starts to grow. It can also be added an additional button that searches across the coordinator if the robot with the specified secret is present, to be used just when the user is recovering a robot and does not remember his coordinator. In this way except when pressing this button the client never makes a requests to every coordinator.

Good to know, my plan with this cache service was also very related to this topic, cache book can also store on goings orders, and if you download it and just search on it your robot name thatś another nice way to know there is an order for your robot, as far the cache system is reliable enought 😄

@jerryfletcher21
Copy link
Contributor

Good to know, my plan with this cache service was also very related to this topic, cache book can also store on goings orders, and if you download it and just search on it your robot name thatś another nice way to know there is an order for your robot, as far the cache system is reliable enought 😄

Ah yes this is even better, if the cache book keeps also track of ongoing orders, the user needs just the secret. The client can calculate the robot name from the secret, then search for his coordinator in the cache book. So there is never the need to search all coordinators to check if the robot is present and have an order.
But I agree is better to first work on making the cache system reliable :)

@KoalaSat KoalaSat force-pushed the use-nostr-as-cache-system branch 2 times, most recently from db49deb to c4452b7 Compare July 16, 2024 11:18
@KoalaSat KoalaSat marked this pull request as ready for review July 19, 2024 10:55
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/1 * * * * torsocks /app/sync.sh
Copy link
Member Author

@KoalaSat KoalaSat Jul 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

torfication of sync calls are done as part of the cron command to avoid mistakes if the coordinator modifies sync.sh https://github.com/RoboSats/robosats/pull/1362/files#diff-366e6549a96aec09107bfc5f3f3804f92cfee8d9804bc8b222136d0a0a15d08b

@KoalaSat KoalaSat force-pushed the use-nostr-as-cache-system branch 2 times, most recently from 9692a7e to 5000155 Compare July 24, 2024 16:36
@KoalaSat KoalaSat force-pushed the use-nostr-as-cache-system branch 6 times, most recently from 7cba270 to b658679 Compare August 7, 2024 20:30
@KoalaSat
Copy link
Member Author

KoalaSat commented Aug 9, 2024

tested and working in Satstralia 👍

@KoalaSat
Copy link
Member Author

KoalaSat commented Sep 6, 2024

Tested in Satstralia for 3 weeks already and fully working! 🚀

@KoalaSat KoalaSat merged commit ce8799d into main Sep 6, 2024
2 checks passed
@KoalaSat KoalaSat deleted the use-nostr-as-cache-system branch September 6, 2024 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants